package com.cy.pj.arithmetic.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class ArithmeticController {
    /*
     * http://localhost/doCompute/10/0
     *
     * 处理异常(解决方案一)：try...catch...
     */
    @RequestMapping("/doCompute/{n1}/{n2}")
    public String doCompute(@PathVariable Integer n1, @PathVariable Integer n2) {
        try {
            Integer result = n1 / n2;
            return "result is " + result;
        } catch(ArithmeticException e) {
            return "doCompute error,caused by " + e.getMessage();
        }
    }

    /*
     * http://localhost/doCompute02/10/2
     *
     * 处理异常(解决方案二)：@ExceptionHandler(ArithmeticException.class)
     */
    @RequestMapping("/doCompute02/{n1}/{n2}")
    public String doCompute02(@PathVariable Integer n1, @PathVariable Integer n2) {
        Integer result = n1 / n2;
        return "result is " + result;
    }

    /*
     * 处理异常(解决方案三)：@RestControllerAdvice
     */
    //@ExceptionHandler(ArithmeticException.class)
    //public String doHandlerArithmeticException(ArithmeticException e) {
    //    return "exception msg is " + e.getMessage();
    //}
}
